% The script is to test whether the systematic error in EEF can influence
% the transfer function. This test uses the function sinu.x to generate
% signals

nsamples = 100000;
nfft = 320;
error_amp_in = 5;
error_amp_out = 2;

% Get IEF values

[x1,ief] = sinu(2 ,0 , error_amp_in, 1,nsamples);

% Get EEF values

[x1,eef] = sinu(2 ,2 , error_amp_out,1,  nsamples);

% Calculate the estimates

[Pxx,F] = pwelch(ief, hanning(nfft),1,nfft,1/5);
[Pxy,F] = cpsd(ief, eef, hanning(nfft),1,nfft,1/5);
[Pyy,F] = pwelch(eef, hanning(nfft),1,nfft,1/5);
[Txy,F] = tfestimate(ief, eef, hanning(nfft),1,nfft,1/5);
[Cxy,F] = mscohere(ief, eef, hanning(nfft),1,nfft,1/5);

close all;
 subplot(311);
semilogy(F,abs(Pxy),'k', 'LineWidth', 2);
set(gca,'FontSize',16);
hold on;
semilogy(F,abs(Pxx),'r', 'LineWidth', 2);
semilogy(F,abs(Pyy),'b', 'LineWidth', 2);
legend('Cross','Auto-IEF','Auto-EEF');
xlabel('Hz');
ylabel('Power');
subplot(312);
 plot(F,abs(Txy),'r', 'LineWidth', 2);
 set(gca,'FontSize',16);
legend('Transfer Function');
xlabel('Hz');
ylabel('Magintude');
 subplot(313);
 plot(F,Cxy,'r', 'LineWidth', 2);
 set(gca,'FontSize',16);
 legend('Coherence');
xlabel('Hz');
ylabel('Coherence');